package com.start.workflow.core.store;

import com.alibaba.fastjson2.JSONObject;
import com.start.common.dataRule.StartQueryWrapper;
import com.start.form.engine.core.StartFormEngine;
import com.start.form.engine.core.TableDataEngineService;
import com.start.form.engine.core.model.StartFormConfigModel;
import com.start.workflow.common.StartActBusForm;
import com.start.workflow.common.enums.StartActHandlerStatus;
import com.start.workflow.common.model.StartActConfigModel;
import com.start.workflow.common.std.IStartActFormStore;
import org.springframework.beans.factory.annotation.Autowired;


public class StartActDynamicFormStore implements IStartActFormStore {

    @Autowired
    private StartFormEngine formEngine;

    /**
     * 表单类型
     *
     * @return
     */
    @Override
    public String actFormType() {
        return "startFormEngine";
    }

    /**
     * 表单类型名称
     *
     * @return
     */
    @Override
    public String actFormTypeLabel() {
        return "系统动态表单";
    }

    /**
     * 加载表单配置
     *
     * @param modelConfig
     * @return
     */
    @Override
    public JSONObject loadOption(StartActConfigModel modelConfig) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        if (form != null) {
            return form.getFormOption();
        }
        return null;
    }


    /**
     * 保存数据
     *
     * @param modelConfig
     * @param procInstId  流程实例id
     * @param status      流程状态
     * @param data
     * @return
     */
    @Override
    public String saveProcFormData(StartActConfigModel modelConfig, String procInstId, StartActHandlerStatus status, JSONObject data) {
        data.put(StartActBusForm.ProcCol.PROCESS_STATE, status.getValue());
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        return tableDataEngineService.save(form, data);

    }

    /**
     * 保存数据
     *
     * @param modelConfig 流程配置
     * @param status      流程状态
     * @param data        需要保存的数据
     * @return
     */
    @Override
    public String saveProcTaskFormData(StartActConfigModel modelConfig, String procInstId, String taskInstId, StartActHandlerStatus status, JSONObject data) {
        data.put(StartActBusForm.ProcCol.PROCESS_STATE, status.getValue());
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        return tableDataEngineService.save(form, data);
    }


    /**
     * 删除流程表单数据
     *
     * @param modelConfig
     * @param procInstId
     */
    @Override
    public void delProcFormData(StartActConfigModel modelConfig, String procInstId) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        tableDataEngineService.remove(form, startQueryWrapper);
    }

    /**
     * 删除流程流程用户任务表单数据
     *
     * @param modelConfig
     * @param procInstId
     * @param taskInstId
     */
    @Override
    public void delProcTaskFormData(StartActConfigModel modelConfig, String procInstId, String taskInstId) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        tableDataEngineService.remove(form, startQueryWrapper);
    }


    /**
     * 获取流程表单的数据
     *
     * @param modelConfig
     * @param procInstId
     * @return
     */
    @Override
    public JSONObject getProcFormData(StartActConfigModel modelConfig, String procInstId) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        return tableDataEngineService.getOne(form, startQueryWrapper);
    }

    @Override
    public JSONObject getProcTaskFormData(StartActConfigModel modelConfig, String procInstId, String taskInstId) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        startQueryWrapper.eq(StartActBusForm.ProcTaskCol.PROCESS_TASK_INST_ID, taskInstId);
        return tableDataEngineService.getOne(form, startQueryWrapper);
    }

    /**
     * 流程状态变化
     *
     * @param modelConfig
     * @param status      状态
     * @param procInstId  流程实例id
     */
    @Override
    public void onActProcStateChange(StartActConfigModel modelConfig, StartActHandlerStatus status, String procInstId) {
        JSONObject tempDate = new JSONObject();
        tempDate.put(StartActBusForm.ProcCol.PROCESS_STATE, status.getValue());
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        tableDataEngineService.update(form, tempDate, startQueryWrapper);
    }

    /**
     * 流程状态变化
     *
     * @param modelConfig
     * @param status      流程状态
     * @param procInstId  流程实例id
     * @param taskInst    数据id
     */
    @Override
    public void onActProcTaskStateChange(StartActConfigModel modelConfig, StartActHandlerStatus status, String procInstId, String taskInst) {
        JSONObject tempDate = new JSONObject();
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        tempDate.put(StartActBusForm.ProcCol.PROCESS_STATE, status.getValue());
        StartQueryWrapper startQueryWrapper = new StartQueryWrapper();
        startQueryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        startQueryWrapper.eq(StartActBusForm.ProcTaskCol.PROCESS_TASK_INST_ID, taskInst);
        formEngine.getTableDataEngineService(form).update(form, tempDate, startQueryWrapper);
    }

    /**
     * 查询指定流程实例指定任务第定义key数据数量
     *
     * @param modelConfig 配置
     * @param procInstId  流程实例id
     * @param taskDefKey  流程定义key
     * @return
     */
    @Override
    public long countProcTaskDataByDefKey(StartActConfigModel modelConfig, String procInstId, String taskDefKey) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper queryWrapper = new StartQueryWrapper();
        queryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        queryWrapper.eq(StartActBusForm.ProcTaskCol.PROCESS_TASK_DEF_KEY, taskDefKey);
        return tableDataEngineService.count(form, queryWrapper);
    }

    /**
     * 删除指定流程实例自定任务定义key数据
     *
     * @param modelConfig 配置
     * @param procInstId  流程实例id
     * @param taskDefKey  流程定义key
     * @return
     */
    @Override
    public long delProcTaskDataByDefKey(StartActConfigModel modelConfig, String procInstId, String taskDefKey) {
        StartFormConfigModel form = formEngine.getForm(modelConfig.getConfVal());
        TableDataEngineService tableDataEngineService = formEngine.getTableDataEngineService(form);
        StartQueryWrapper queryWrapper = new StartQueryWrapper();
        queryWrapper.eq(StartActBusForm.ProcCol.PROCESS_INSTANCE_ID, procInstId);
        queryWrapper.eq(StartActBusForm.ProcTaskCol.PROCESS_TASK_DEF_KEY, taskDefKey);
        return tableDataEngineService.remove(form, queryWrapper);
    }
}
